/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.databinding.ui.event.IEvent;
import org.leeloo.internal.core.ObjectDescriptor;

/**
 * Provides labels according to the following behavior:
 * <ul>
 * <li>First column will be the {@link LabeledTreeNode#getLabel()} of the node.</li>
 * <li>Values of non ObjectDescriptors or Events will be displayed in the
 * second column. If no value exists a standard placeholder is provided.</li>
 * </ul>
 * 
 * @author Brad Reynolds
 */
public class LabeledTreeNodeLabelProvider extends LabelProvider implements ITableLabelProvider {
    private final TagService tagService;
    
    /**
     * Value to display for nodes that have no value.
     */
    public final static String NO_VALUE = Messages.getString("LabeledTreeNodeLabelProvider.unavailable"); //$NON-NLS-1$
    /**
     * Value to display for nodes that have no value and are editable.
     */
    public final static String SELECT_TO_EDIT = Messages.getString("LabeledTreeNodeLabelProvider.select_to_edit"); //$NON-NLS-1$

    public LabeledTreeNodeLabelProvider(TagService tagService) {
        if (tagService == null) {
            throw new IllegalArgumentException("Parameter tagService was null."); //$NON-NLS-1$
        }
        
        this.tagService = tagService;
    }

    /**
     * Images are not support at this time, does nothing.
     * @param element 
     * @param columnIndex 
     * @return null
     */
    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    /**
     * Returns the text to display for the 2 columns of the viewer.
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
     */
    public String getColumnText(Object element, int columnIndex) {
        String result = null;

        if (element instanceof LabeledTreeNode) {
            LabeledTreeNode node = (LabeledTreeNode) element;
            Object value = node.getValue();

            switch (columnIndex) {
                case 0:
                    result = node.getLabel();
                    break;
                case 1:
                    if (value != null) {
                        if (node instanceof LabeledTagTreeNode) {
                            String tagName = ((LabeledTagTreeNode) node).getTag().getName();
                            Tag tag = tagService.get(value, tagName); 
                            if (tag != null) {
                                result = tag.getValue();
                            }
                            if (result == null || result.length() == 0) {
                                result = SELECT_TO_EDIT;
                            }
                        } else if (!(value instanceof ObjectDescriptor || value instanceof IEvent)) {
                            result = value.toString();
                        }
                    } else {
                        result = NO_VALUE;
                    }
                    break;
            }
        }

        return result;
    }
}
